<template>
  <div class="app-container">
    <el-row class="elRow">
      <el-col>
        <el-button class="mb-2 mr-3" type="primary" @click="add"
          ><el-icon class="mr-1" color="#fff"><Plus /></el-icon>新增</el-button
        >
      </el-col>
    </el-row>
    <!-- 列表 -->
    <el-table ref="table" v-loading="loading" :data="data" :height="height" @sort-change="sort">
      <el-table-column prop="real_amount" label="充值金额(元)" />
      <el-table-column prop="wechat_info.title" label="有效期" />
      <el-table-column prop="phone" label="赠送内容" show-overflow-tooltip />
      <el-table-column
        v-if="checkPermission(['admin/member.Member/disable'])"
        prop="is_disable"
        label="是否启用"
      >
        <template #default="scope">
          <el-switch
            v-model="scope.row.is_disable"
            :active-value="1"
            :inactive-value="0"
            @change="disable([scope.row])"
          />
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="排序" />
      <el-table-column prop="create_time" label="创建时间" />
      <el-table-column label="操作" width="180" fixed="right">
        <template #default="scope">
          <el-link type="primary" class="mr-1" :underline="false" @click="edit"> 编辑 </el-link>
          <el-link type="primary" class="mr-1" :underline="false"> 删除 </el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="count > 0"
      v-model:total="count"
      v-model:page="query.page"
      v-model:limit="query.limit"
      @pagination="list"
    />

    <!-- 添加修改 -->
    <el-dialog
      v-model="dialog"
      :title="dialogTitle"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="cancel"
      top="5vh"
    >
      <el-form ref="ref" :model="model" :rules="rules" label-width="100px">
        <el-form-item label="充值金额" prop="nickname">
          <el-input
            key="nickname"
            v-model="model.nickname"
            placeholder="请输入充值金额"
            clearable
          />
        </el-form-item>
        <el-form-item label="有效期" prop="username">
          <el-date-picker
            v-model="query.date_value"
            type="datetimerange"
            class="ya-date-value mr-6"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :shortcuts="dateShortcuts"
            @change="search()"
            popper-class="datetimerange-style"
            value-format="YYYY-MM-DD HH:mm:ss"
            :default-time="[new Date(2024, 1, 1, 0, 0, 0), new Date(2024, 1, 1, 23, 59, 59)]"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="model.sort" type="number" />
        </el-form-item>
        <el-form-item label="赠送类型">
          <el-checkbox-group v-model="model.market_ids">
            <el-checkbox label="金额">
              <div style="display: flex; align-items: center">
                金额
                <el-tooltip
                  class="box-item"
                  :content="`敬请期待`"
                  effect="light"
                  raw-content
                  placement="top-start"
                >
                  <el-icon class="ml-0.5" color="#979797" size="18"><QuestionFilled /></el-icon>
                </el-tooltip>
              </div>
            </el-checkbox>
            <el-checkbox label="AI模型权益">
              <div style="display: flex; align-items: center">
                AI模型权益
                <el-tooltip
                  class="box-item"
                  :content="`敬请期待`"
                  effect="light"
                  raw-content
                  placement="top-start"
                >
                  <el-icon class="ml-0.5" color="#979797" size="18"><QuestionFilled /></el-icon>
                </el-tooltip>
              </div>
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="赠送金额" prop="phone">
          <el-input v-model="model.phone" clearable />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button :loading="loading" @click="cancel">取消</el-button>
        <el-button :loading="loading" type="primary" @click="submit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import screenHeight from '@/utils/screen-height'
import Pagination from '@/components/Pagination/index.vue'
import { getPageLimit } from '@/utils/settings'
import { list, add, edit, dele, disable } from '@/api/member/member'
export default {
  name: 'RechargeIndex',
  components: { Pagination },
  data() {
    return {
      query: {
        page: 1,
        limit: getPageLimit()
      },
      data: [],
      count: 0,
      dialog: false,
      dialogTitle: '',
      model: {},
      rules: {
        nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.height = screenHeight()
    this.list()
  },
  methods: {
    checkPermission,
    // 列表
    list() {
      this.loading = true
      list(this.query)
        .then((res) => {
          this.data = res.data.list
          this.count = res.data.count
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 添加修改
    add() {
      this.dialog = true
      this.dialogTitle = '添加'
    },
    edit(row) {
      this.dialog = true
      this.dialogTitle = '修改'
    },
    cancel() {
      this.dialog = false
    },
    submit() {
      this.$refs['ref'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.model.api_tree = []
          this.model.thirds = []
          if (this.model[this.idkey]) {
            edit(this.model)
              .then((res) => {
                this.list()
                this.dialog = false
                ElMessage.success(res.msg)
              })
              .catch(() => {
                this.loading = false
              })
          } else {
            add(this.model)
              .then((res) => {
                this.list()
                this.dialog = false
                ElMessage.success(res.msg)
              })
              .catch(() => {
                this.loading = false
              })
          }
        } else {
          ElMessage.error('请完善必填项（带红色星号*）')
        }
      })
    },
    // 是否启用
    disable(row, select = false) {
      this.loading = true
      var is_disable = row[0].is_disable
      if (select) {
        is_disable = this.is_disable
      }
      disable({
        ids: this.selectGetIds(row),
        is_disable: is_disable
      })
        .then((res) => {
          this.list()
          ElMessage.success(res.msg)
        })
        .catch(() => {
          this.list()
        })
    },
    // 删除
    dele(row) {
      this.loading = true
      dele({
        ids: this.selectGetIds(row)
      })
        .then((res) => {
          this.list()
          ElMessage.success(res.msg)
        })
        .catch(() => {
          this.loading = false
        })
    }
  }
}
</script>
